package com.whut.whw.message;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.whut.whw.R;
import com.whut.whw.util.EmptyUtils;
import com.whut.whw.util.GlideCircularLoadUtils;

import java.util.List;

/**
 * <pre>
 *  desc:
 *  Created by 忘尘无憾 on 2017/06/14.
 *  version:
 * </pre>
 */

public class MsgRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<MsgInformModel> informList;
    private Context context;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private OnItemClickListener onItemClickListener;
    /**
     * 0 @我
     * 1 通知/关注
     * 默认是@我
     */
    private int flag = 0;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);

        void onImgClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public MsgRecyclerViewAdapter(Context context, List<MsgInformModel> informList) {
        this.informList = informList;
        this.context = context;
    }

    /**
     * 构造函数，多传入一个flag，用于判断是@我的内容还是关注和通知
     * @param context
     * @param informList
     * @param flag
     */
    public MsgRecyclerViewAdapter(Context context, List<MsgInformModel> informList, int flag) {
        this(context, informList);
        this.flag = flag;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            if(flag==0){//如果是@我
                View view = LayoutInflater.from(context).inflate(R.layout.fragment_msg_card_item, parent, false);
                return new MsgCardHolder(view);
            }else if(flag==1){//如果是关注/通知
                View view = LayoutInflater.from(context).inflate(R.layout.fragment_msg_follow_item, parent, false);
                return new MsgNotifyHolder(view);
            }
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.default_loading, parent, false);
            return new FootHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        setListent(holder);
        if (holder instanceof MsgCardHolder) {
            setCardData(holder,position);//设置是@我的列表显示等
        }else if(holder instanceof MsgNotifyHolder){
            setNotifyData(holder,position);//设置我关注或通知的列表
        }
    }

    /**
     * 设置点击、长按监听
     * @param holder
     */
    private void setListent(final RecyclerView.ViewHolder holder) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getLayoutPosition();
                onItemClickListener.onItemClick(holder.itemView, position);
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int position = holder.getLayoutPosition();
                onItemClickListener.onItemLongClick(holder.itemView, position);
                return true;
            }
        });
    }

    /**
     * 设置@我的列表显示
     * @param holder
     * @param position
     */
    private void setCardData(final RecyclerView.ViewHolder holder,int position){

        MsgInformModel informModel = this.informList.get(position);//获取每项数据
        MsgCardHolder cardHolder = (MsgCardHolder) holder;//强转为MsgCardHolder

        //设置头像点击事件
        cardHolder.msg_card_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getLayoutPosition();
                onItemClickListener.onImgClick(holder.itemView, position);
            }
        });

        //判断头像imgurl是否为空
        if (EmptyUtils.isEmpty(informModel.getMsgImgUrl())) {
            //为空则getMsgIcon，是从资源文件获取的
            Glide
                    .with(this.context)
                    .load(informModel.getMsgIcon())
                    .asBitmap().centerCrop()
                    .into(new GlideCircularLoadUtils(this.context, cardHolder.msg_card_icon));
        } else {
            //不为空则从网络地址获取
            Glide
                    .with(this.context)
                    .load(informModel.getMsgImgUrl())
                    .asBitmap().centerCrop()
                    .into(new GlideCircularLoadUtils(this.context, cardHolder.msg_card_icon));
        }

        cardHolder.msg_card_name.setText(informModel.getMsgName());//设置用户名
        cardHolder.msg_card_time.setText(informModel.getMsgTime());//设置时间
        if ("0".equals(informModel.getType())) {//回复/评论
            cardHolder.msg_card_comment.setText(informModel.getMsgContentFlag());//设置评论或回复
            cardHolder.msg_card_content.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);//设置内容的字体大小为14dp
            cardHolder.msg_card_content.setTextColor(ContextCompat.getColor(context, R.color.text_555));//设置内容的颜色为#555
            cardHolder.msg_card_content.setText(informModel.getMsgContent());//设置内容
        } else if ("1".equals(informModel.getType())) {//点赞
            cardHolder.msg_card_comment.setText("");//设置不显示评论或回复字段
            cardHolder.msg_card_content.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);//设置内容的字体大小为15dp，和用户名大小一样
            cardHolder.msg_card_content.setTextColor(ContextCompat.getColor(context, R.color.text_444));//设置内容的颜色为#444
            cardHolder.msg_card_content.setText(informModel.getMsgContent());//设置内容
        }

        //判断动态是否为空
        if (EmptyUtils.isEmpty(informModel.getMsgDynamic())) {
            cardHolder.msg_card_dynamic.setVisibility(View.GONE);//如果为空则使该条不显示
        } else {
            cardHolder.msg_card_dynamic.setVisibility(View.VISIBLE);//否则显示
            cardHolder.msg_card_dynamic.setText(informModel.getMsgDynamic());//设置动态内容
        }
    }

    /**
     * 设置关注或通知的列表
     * @param holder
     * @param position
     */
    private void setNotifyData(final RecyclerView.ViewHolder holder,int position){

        MsgInformModel informModel = this.informList.get(position);//获取数据

        MsgNotifyHolder notifyHolder = (MsgNotifyHolder) holder;//强转为MsgNotifyHolder

        //设置头像点击事件
        notifyHolder.msg_notify_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getLayoutPosition();
                onItemClickListener.onImgClick(holder.itemView, position);
            }
        });
        //判断imgurl是否为空
        if (EmptyUtils.isEmpty(informModel.getMsgImgUrl())) {
            Glide
                    .with(this.context)
                    .load(informModel.getMsgIcon())
                    .asBitmap().centerCrop()
                    .into(new GlideCircularLoadUtils(this.context, notifyHolder.msg_notify_img));
        } else {
            Glide
                    .with(this.context)
                    .load(informModel.getMsgImgUrl())
                    .asBitmap().centerCrop()
                    .into(new GlideCircularLoadUtils(this.context, notifyHolder.msg_notify_img));
        }

        //设置用户名、内容、时间
        notifyHolder.msg_notify_name.setText(informModel.getMsgName());
        notifyHolder.msg_notify_content.setText(informModel.getMsgContent());
        notifyHolder.msg_notify_time.setText(informModel.getMsgTime());
    }

    @Override
    public int getItemViewType(int position) {
        //判断是否是最后一个，因为position是从0开始所以+1
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        /**
         * 判断informList是否为空
         * 如果不为空，则在该长度上多加一行，可用于上拉加载
         */
        return EmptyUtils.isEmpty(informList.size()) ? 0 : informList.size() + 1;
    }

    /**
     * <pre>
     *  @Decription 用于消息卡片中内容的初始化
     *  @datetime 2017/06/15 9:22
     *  @author 忘尘无憾
     *  @version
     * </pre>
     */
    class MsgCardHolder extends RecyclerView.ViewHolder {

        private CardView msg_card_view;//card
        private ImageView msg_card_icon;//头像
        private TextView msg_card_name;//用户名
        private TextView msg_card_time;//时间
        private TextView msg_card_comment;//评论/回复
        private TextView msg_card_content;//评论或回复内容
        private TextView msg_card_dynamic;//动态

        public MsgCardHolder(View itemView) {
            super(itemView);
            msg_card_view = (CardView) itemView.findViewById(R.id.msg_card_view);
            msg_card_icon = (ImageView) itemView.findViewById(R.id.msg_card_icon);
            msg_card_name = (TextView) itemView.findViewById(R.id.msg_card_name);
            msg_card_time = (TextView) itemView.findViewById(R.id.msg_card_time);
            msg_card_comment = (TextView) itemView.findViewById(R.id.msg_card_comment);
            msg_card_content = (TextView) itemView.findViewById(R.id.msg_card_content);
            msg_card_dynamic = (TextView) itemView.findViewById(R.id.msg_card_dynamic);
        }
    }

    class MsgNotifyHolder extends RecyclerView.ViewHolder{
        private ImageView msg_notify_img;
        private TextView msg_notify_name;
        private TextView msg_notify_content;
        private TextView msg_notify_time;

        public MsgNotifyHolder(View itemView) {
            super(itemView);
            msg_notify_img = (ImageView) itemView.findViewById(R.id.msg_notify_img);
            msg_notify_name = (TextView) itemView.findViewById(R.id.msg_notify_name);
            msg_notify_content = (TextView) itemView.findViewById(R.id.msg_notify_content);
            msg_notify_time = (TextView) itemView.findViewById(R.id.msg_notify_time);
        }
    }

    /**
     * <pre>
     *  @Decription 底部的正在加载
     *  @datetime 2017/06/15 9:29
     *  @author 忘尘无憾
     *  @version
     * </pre>
     */
    static class FootHolder extends RecyclerView.ViewHolder {

        public FootHolder(View itemView) {
            super(itemView);
        }
    }
}
